package com.example.api;

import com.example.entity.SwaggerUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "用户端控制器")
@RestController
@RequestMapping("/knife")
public class ApiController {

    @ApiOperation(value = "获取数据")
    @GetMapping("/api/test")
    public List<SwaggerUser> test(@ApiParam(name = "id", value = "用户Id") String id,
                                  @ApiParam(name = "name", value = "用户名称") String name) {

        List<SwaggerUser> list = new ArrayList<>();
        list.add(SwaggerUser.builder().id(id).name(name).build());
        return list;
    }

    @ApiOperation("@PathVariable接收")
    @GetMapping("/api/PathVariable/{id}")
    public List<SwaggerUser> PathVariable(@PathVariable String id) {

        List<SwaggerUser> list = new ArrayList<>();
        list.add(SwaggerUser.builder().id(id).build());
        return list;
    }

    @ApiOperation("@RequestParam接收")
    @GetMapping("/api/RequestParam")
    public List<SwaggerUser> RequestParam(@RequestParam String id,
                                          @RequestParam String name) {

        List<SwaggerUser> list = new ArrayList<>();
        list.add(SwaggerUser.builder().id(id).name(name).build());
        return list;
    }

    @ApiOperation("@RequestBody接收")
    @PostMapping("/api/RequestBody")
    public List<SwaggerUser> RequestBody(@RequestBody SwaggerUser swaggerUser) {

        List<SwaggerUser> list = new ArrayList<>();
        list.add(swaggerUser);
        return list;
    }

    @ApiOperation("对象直接接收")
    @GetMapping("/api/direct")
    public List<SwaggerUser> direct(SwaggerUser swaggerUser) {
        List<SwaggerUser> list = new ArrayList<>();
        list.add(swaggerUser);
        return list;
    }

    @ApiOperation(value = "示例：导入-方式一")
    @PostMapping("/importDate")
    //@ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class, required = true)
    //@RequestPart("file")
    public boolean importDate(MultipartFile file, String id) {
        String name = file.getName();
        System.out.println("name = " + name);
        return true;
    }

    @GetMapping("/export")
    @ApiOperation(value = "示例：导出-方式一", produces = "application/octet-stream")
    public Boolean export() {
        return true;
    }
}